//
//  LoginModel.m
//  Tiipvi
//
//  Created by xiaowuxiaowu on 2017/5/2.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "LoginModel.h"
#import "TPLoginViewController.h"
@implementation LoginModel
@synthesize pass  = _pass;
+(instancetype)shareLoginModel{
 
    static LoginModel *model;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        model = [LoginModel new];
        model.loginType = OtherLoginTypeDefaultPsw;
        [model getLoginModel];
//        model.uid = 909;
    });
    return model;
    
}

-(BOOL)isSetPsw{
    return [self.has_password isEqualToString:@"yes"];
}

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper{
    return @{@"uid" : @"id"};
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if([key isEqualToString:@"id"])
        self.userId = [value integerValue];
}
-(void)setNilValueForKey:(NSString *)key{
    
}
+(void)LoginSuccess:(NSDictionary *)dict type:(OtherLoginType)type{
  
    
    LoginModel *model = [LoginModel yy_modelWithDictionary:dict[@"data"]];
    model.refreshToken = xw_getNULLString([dict objectForKey:@"refresh_token"]);
    model.access_token = xw_getNULLString([dict objectForKey:@"access_token"]);
    [[NSUserDefaults standardUserDefaults]setObject:@(type) forKey:kLogin_user_loginType];
    [model save];
    
    [[LoginModel shareLoginModel]getLoginModel];
    
}

-(void)setPass:(NSString *)pass{
    _pass = pass;
    [[NSUserDefaults standardUserDefaults]setObject:xw_getNULLString(pass) forKey:kLogin_user_pass];
}
-(NSString *)pass{
    return [[NSUserDefaults standardUserDefaults]objectForKey:kLogin_user_pass];
}
-(OtherLoginType)loginType{
    
    id type = [[NSUserDefaults standardUserDefaults]objectForKey:kLogin_user_loginType];
    if(type == nil){
        return OtherLoginTypeDefaultPsw;
    }
    
    return ((NSNumber *)type).integerValue;
}
//-(NSString *)token{
//  return @"40CE973AB37F4D8781EE6068A98D3001";
//}
-(BOOL)isLogin{
    return self.userId > 0;
}

-(void)getLoginModel{
    
    NSString *json = [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginModel"];
     [self yy_modelSetWithJSON:json];
//    [self setValuesForKeysWithDictionary:dic];
    DLog(@"\n登录判读----%@\n",json);
    //
    //    if([dic[@"id"] integerValue] > 0)
    //    self.uid = [dic[@"id"] integerValue];
}
-(void)save{
    
    [[NSUserDefaults standardUserDefaults]setObject:[self yy_modelToJSONString] forKey:@"LoginModel"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
}
-(void)clear{
    
    self.access_token = @"";
    self.refreshToken = @"";
    self.userId = 0;
    self.user_name = @"";
    self.pass = @"";
    self.head = @"";
    
    [[NSUserDefaults standardUserDefaults]setObject:@{} forKey:@"LoginModel"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self getLoginModel];
    
}

-(void)getInfoModel{
    
    
      
    [[TPRequestClient sharedClient]getInfo_success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        NSDictionary *dict = respone.data;
        [self setValuesForKeysWithDictionary:dict];
      
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        
          [self clear];
//        [UIView xw_hideTitle:respone.message];
//        [XWCommon shareAppDelegate].window.rootViewController = [[UINavigationController alloc]initWithRootViewController:       [TPLoginViewController new]];
        
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        ;
    }];
    
}

@end


BOOL tp_isLogin(){
    
   if(![LoginModel shareLoginModel].isLogin)
    [[XWCommon shareAppDelegate].window.rootViewController presentViewController:[[UINavigationController alloc] initWithRootViewController:[TPLoginViewController new]] animated:YES completion:nil];
    
    return [LoginModel shareLoginModel].isLogin;
    
}
